package com.wl.org.service.impl;

import com.wl.basic.service.impl.BaseServiceImpl;
import com.wl.basic.util.Md5Utils;
import com.wl.basic.util.PageList;
import com.wl.basic.util.StrUtils;
import com.wl.org.domain.Employee;
import com.wl.org.mapper.EmployeeMapper;
import com.wl.org.query.EmployeeQuery;
import com.wl.org.service.EmployeeService;
import com.wl.user.domain.Logininfo;
import com.wl.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void save(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //保存logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);

        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);
        //判断
        if (employee.getRole_id()!= null){
            //保存中间表的信息
            employeeMapper.saveEmployeeRole(employee);
        }
    }


    @Override
    @Transactional
    public void remove(Long id) {
        //删除登录信息logininfo
        Employee employee = employeeMapper.loadById(id);
        logininfoMapper.remove(employee.getLogininfo_id());

        //删除t_employee:删除中间表信息：t_employee_role
        employeeMapper.removeEmployeeRoleByEmployeeId(id);
        //删除employee信息
        super.remove(id);

    }


    @Override
    @Transactional
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        super.update(employee);

        //获取登录信息id
        Long logininfoId = employeeMapper.loadById(employee.getId()).getLogininfo_id();

        //创建logininfo
        Logininfo logininfo = logininfoMapper.loadById(logininfoId);//id,type ,state
        //将新的数据覆盖旧的数据
        BeanUtils.copyProperties(employee,logininfo);//5个数据新的 - 用户名，邮箱，电话，盐值，密码
        //重置logininfoId
        logininfo.setId(logininfoId);
        logininfoMapper.update(logininfo);

        //删除中间表信息
        employeeMapper.removeEmployeeRoleByEmployeeId(employee.getId());
        //保存新的信息
        if (employee.getRole_id()!= null){
            //保存中间表的信息
            employeeMapper.saveEmployeeRole(employee);
        }
        super.update(employee);
    }

    /**
     * 分页
     * @param employeeQuery
     * @return
     */
    @Override
    public PageList<Employee> queryPage(EmployeeQuery employeeQuery) {
        Integer totals = employeeMapper.queryCount(employeeQuery);
        if(totals==0){
            return new PageList<>();
        }
        List<Employee> data = employeeMapper.queryData(employeeQuery);
        return new PageList<>(totals,data);
    }


}
